package dp.mobile.entity.tower;

import java.io.Serializable;

import org.w3c.dom.Node;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import dp.mobile.entity.BulletData;
import dp.mobile.glwrapper.GLTexture;
import dp.mobile.glwrapper.GameRenderer;
import dp.mobile.system.Utilities;

public class TowerData implements Serializable {
	private static final String TAG = "Tower Data";
	
	public TowerData() {
		//Initialize
		mName			= "Tower";
		mBuildCost		= 0;
		mDismantleCost	= 0;
		mIndex			= 0;
		mRange			= 0;
		mReload			= 0;
		mImage			= null;
		mBullet			= null;
		mImageFile		= "";
		mDescription	= "";
		mUpgradeTime	= 0;
		mUpgrade		= null;
		mWorth			= new int[] {0, 0, 0};
	}

	public TowerData(Node root) {
		//Default
		this();
		
		String upgrade = null;

		//For each data
		for (int i = 0; i < root.getChildNodes().getLength(); i++) {
			//Get variable
			Node child 		= root.getChildNodes().item(i);
			if (child.getNodeType() == Node.TEXT_NODE) continue;
			
			String tag		= child.getNodeName();
			String value	= child.getFirstChild().getNodeValue();

			//Check name
			if (tag.equals("description"))	mDescription 	= value;
			else if(tag.equals("name"))		mName 			= value;
			else if (tag.equals("index"))	mIndex 			= Integer.parseInt(value);
			else if (tag.equals("bullet"))	mBullet 		= new BulletData(child);
			else if (tag.equals("reload"))	mReload 		= Integer.parseInt(value);
			else if (tag.equals("time"))	mUpgradeTime 	= Integer.parseInt(value);
			else if (tag.equals("upgrade"))	upgrade			= value;
			else if (tag.equals("build-cost"))		mBuildCost 		= Integer.parseInt(value);
			else if (tag.equals("dismantle-cost"))	mDismantleCost 	= Integer.parseInt(value);
			else if (tag.equals("image")) {
				//Create image
				mImageFile = value;
				mImage	= GameRenderer.instance().allocateTexture(Utilities.TOWER_FOLDER + "/" + mImageFile + ".png");
				//mIcon	= BitmapFactory.decodeFile(Utilities.TOWER_FOLDER + "/" + mImageFile + "-icon.png");
				mIcon	= BitmapFactory.decodeFile(Utilities.TOWER_FOLDER + "/" + mImageFile + ".png");
			} else if (tag.equals("range"))	{
				//Set range
				mRange				= Integer.parseInt(value);
			} else if (tag.equals("worth"))	{
				int grandChildCount = child.getChildNodes().getLength();
				
				for (int j = 0; j < grandChildCount; ++j) {
					Node worthChild = child.getChildNodes().item(j);
					if (worthChild.getNodeType() == Node.TEXT_NODE) continue;
					
					String childTag = worthChild.getNodeName();
					String childVal = worthChild.getFirstChild().getNodeValue();
					int type		= 0;
					
					if (childTag.equals("component")) {
						if (worthChild.getAttributes().getNamedItem("type") != null) {
							type = Integer.parseInt(worthChild.getAttributes().getNamedItem("type").getNodeValue());
							mWorth[type] = Integer.parseInt(childVal);
						}
					}
				}
			}
		}
		
		if (upgrade != null) {
			upgrade = upgrade.trim();
			String[] tempUpgrade = upgrade.split(";");
			
			mUpgrade = new int[tempUpgrade.length];
			for (int i = 0; i < tempUpgrade.length; ++i)
				mUpgrade[i] = Integer.parseInt(tempUpgrade[i]);
		}
	}

	//Accessors
	public int getBuildCost()		{	return mBuildCost;				}
	public int getDismantleCost()	{	return mDismantleCost;			}
	public int getWorth(int num)	{	return mWorth[num];				}
	public int getUpgrade(int num)	{	return mUpgrade[num];				}
	public int getRange()			{	return mRange;					}
	public Bitmap getIcon()			{	return mIcon;					}
	public long getReload()			{	return mReload;					}
	public GLTexture getImage()		{	return mImage;					}
	public String getName()			{	return mName;					}
	public String getImageFile()	{	return mImageFile;				}
	public String getDescription()	{	return mDescription;			}
	public BulletData getBullet()	{	return mBullet;					}
	public int getUpgradeTime()		{	return mUpgradeTime;			}
	public int getIndex()			{	return mIndex;					}

	//Data
	protected int		mIndex;
	protected int		mBuildCost, mDismantleCost;
	protected String	mName;
	protected Bitmap	mIcon;
	protected GLTexture	mImage;
	protected String	mImageFile;
	protected String	mDescription;
	protected int		mUpgradeTime;
	protected int[]		mWorth;
	protected int[]		mUpgrade;

	//Attack data
	protected int			mRange;
	protected long			mReload;
	protected BulletData	mBullet;
}